-module(intersect).

-include("geometry.hrl").

-export([is_intersectant/2]).

-include_lib("eunit/include/eunit.hrl").

-spec is_intersectant(bbox(),bbox()) -> bool().
is_intersectant(#bbox{left=X11,top=Y11,right=X12,bottom=Y12}, #bbox{left=X21,top=Y21,right=X22,bottom=Y22}) ->
    ((X21 =< X12) and (X11 =< X22)
    and
     (Y21 >= Y12) and (Y11 >= Y22)).

box_intersect_test() ->
    B1 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B2 = #bbox{left=4.0,top=6.0,right=9.0,bottom=3.0},
    ?assert(is_intersectant(B1,B2)),
    ?assert(is_intersectant(B2,B1)),
    ?assert(is_intersectant(B2,B2)),
    ?assert(is_intersectant(B1,B1)),

    B5 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B6 = #bbox{left=4.0,top=16.0,right=9.0,bottom=13.0},
    ?assertNot(is_intersectant(B5,B6)),
    ?assertNot(is_intersectant(B6,B5)),
    
    B7 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B8 = #bbox{left=4.0,top=3.0,right=9.0,bottom=0.1},
    ?assert(is_intersectant(B7,B8)),
    ?assert(is_intersectant(B8,B7)),
    
    B9 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B10 = #bbox{left=-0.1,top=3.0,right=4.1,bottom=0.1},
    ?assert(is_intersectant(B9,B10)),
    ?assert(is_intersectant(B10,B9)),
    
    B11 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B12 = #bbox{left=0.1,top=6.0,right=4.0,bottom=3.0},
    ?assert(is_intersectant(B11,B12)),
    ?assert(is_intersectant(B12,B11)),
    
    B13 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B14 = #bbox{left=0.2,top=6.0,right=9.0,bottom=0.2},
    ?assert(is_intersectant(B13,B14)),
    ?assert(is_intersectant(B14,B13)),
    
    B3 = #bbox{left=1.0,top=5.0,right=7.0,bottom=2.0},
    B4 = #bbox{left=14.1,top=6.1,right=19.1,bottom=3.1},
    ?assertNot(is_intersectant(B3,B4)),
    ?assertNot(is_intersectant(B4,B3)).
    

